package com.tanhua.dubbo.api.impl;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.vo.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService(version = "1.0.0")
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserLikeApi userLikeApi;

    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        //按照得分倒序排序，获取得分最高的那个用户
        Query query = Query.query(Criteria.where("toUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return this.mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long userId, Integer pageNum, Integer pageSize) {

        PageInfo<RecommendUser> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);

        //分页参数
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize,
                Sort.by(Sort.Order.desc("score")));

        //查询今日佳人
        RecommendUser recommendUser = this.queryWithMaxScore(userId);
        //查询条件
        Query query = Query.query(Criteria.where("toUserId").is(userId).and("score").lt(recommendUser.getScore())).with(pageRequest);
        List<RecommendUser> recommendUserList = this.mongoTemplate.find(query, RecommendUser.class);
        if (CollUtil.isEmpty(recommendUserList)) {
            return pageInfo;
        }

        pageInfo.setRecords(recommendUserList);
        // pageInfo.setTotal();//暂时不提供数据总条数

        return pageInfo;
    }

    @Override
    public Double queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId)).limit(1);
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (null != recommendUser) {
            return recommendUser.getScore();
        } else {
            return 80d;
        }
    }

    /**
     * 查询探花列表，查询时需要排除喜欢和不喜欢的用户
     *
     * @param userId
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryCardList(Long userId, Integer count) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        //查询用户的喜欢和不喜欢列表
        List<Long> likeList = this.userLikeApi.queryLikeList(userId);
        List<Long> notLikeList = this.userLikeApi.queryNotLikeList(userId);

        List<Long> idList = new ArrayList<>();
        idList.addAll(likeList);
        idList.addAll(notLikeList);

        if(CollUtil.isNotEmpty(idList)){
            //排除已喜欢和不喜欢的用户
            criteria.andOperator(Criteria.where("userId").nin(idList));
        }

        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score"))).limit(count);
        return this.mongoTemplate.find(query, RecommendUser.class);
    }
    /**
     * 查询默认推荐探花列表，查询时需要排除喜欢和不喜欢的用户
     *
     * @param idList 推荐探花列表
     * @return
     */

    @Override
    public List<RecommendUser> queryCardListDefault(Long userId,List idList) {
        //查询用户的喜欢和不喜欢列表
        List<Long> likeList = this.userLikeApi.queryLikeList(userId);
        List<Long> notLikeList = this.userLikeApi.queryNotLikeList(userId);
        //排除已喜欢和不喜欢的用户
        idList.removeAll(likeList);
        idList.removeAll(notLikeList);

        Query query = Query.query(Criteria.where("userId").in(idList))
                .with(Sort.by(Sort.Order.desc("score")));
        return this.mongoTemplate.find(query, RecommendUser.class);
    }
}
